import { t } from 'i18next'
import { Table, ColumnProps } from '@kedacom/react-next'
import React, { useMemo } from 'react'
import { Title, BlueLinkSpan, StyledBottom, Wrap } from './style'
import { Alert } from '@/components/Alert'
import { API_MEETING_PREFIX } from '@/utils/constant'
import { useRequest } from 'ahooks'
import { fetchGetMeetingFiles } from '@/api/meetingSpace/docs'
import { ITableData, transMeetingDocItem } from './translator'
import { IMeetingInfo } from '@/api/meetingSpace/interface'

const Docs = ({ meetingInfo }: { meetingInfo: IMeetingInfo }) => {
	const Columns = useMemo<ColumnProps<ITableData>[]>(
		() => [
			{
				dataKey: 'fileName',
				title: t('文件名称'),
				width: 340,
			},
			{
				dataKey: 'creator',
				title: t('上传人员'),
				width: 160,
			},
			{
				dataKey: 'createTime',
				title: t('上传时间'),
				width: 180,
			},
			{
				dataKey: 'fileSize',
				title: t('大小'),
				width: 130,
			},
			{
				dataKey: 'status',
				title: t('状态'),
				width: 150,
				render: (item) => {
					return item.fileUrl ? '' : <span>{t('文档已失效')}</span>
				},
			},
			{
				dataKey: 'action',
				title: t('操作'),
				width: 60,
				render: (item) => {
					return (
						<BlueLinkSpan
							onClick={(e) => {
								e.stopPropagation()
								DownloadFile(item)
							}}
						>
							{t('下载')}
						</BlueLinkSpan>
					)
				},
			},
		],
		[]
	)
	const { data, loading } = useRequest(() => fetchGetMeetingFiles(meetingInfo.meetingMoid))
	const tableData = useMemo(() => {
		if (!loading && data) {
			return data.map((x) => transMeetingDocItem(x))
		}
		return []
	}, [data, loading])
	const DownloadFile = (item: ITableData) => {
		if (item.status === '0') {
			Alert(t('文件服务器不存在'))
		} else if (item.status === '2') {
			Alert(t('文件服务器不可访问'))
		} else {
			window.open(`${API_MEETING_PREFIX}/meeting_files/${item.fileId}/brower`)
		}
	}
	return (
		<Wrap>
			<Title>
				<div className="doc-count">{t('文件列表（{{num}}）', { num: tableData.length })}</div>
			</Title>
			<Table
				height={488}
				rowIdField={'fileId'}
				data={tableData}
				columns={Columns}
				showOrderColumn={false}
				showSelectColumn={false}
				i18n={{ order: t('序号') }}
				nodata={t('暂无数据')}
			/>
			<StyledBottom />
		</Wrap>
	)
}
export default Docs
